package com.magical.ydyykfsy2v2;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.os.Handler;
import android.os.Message;
public class MainActivity extends AppCompatActivity {
    ImageView img = null;
    Button but1 = null,but2 = null, but3 = null, but4 = null;
    boolean flag;//用于自动播放和停止
    int count = 0;
    private Animation mAnimation;
    private MediaPlayer mediaPlayer;
    String msg1="Android:";
    int[] imgRes = {
            R.drawable.img00,
            R.drawable.img01,
            R.drawable.img02,
            R.drawable.img03,
            R.drawable.img04,
            R.drawable.img05,
            R.drawable.img06,
            R.drawable.img07,
            R.drawable.img08,
            R.drawable.img09,
            R.drawable.img10,
            R.drawable.img11,
            R.drawable.img12,
            R.drawable.img13,
            R.drawable.img14,
            R.drawable.img15,
            R.drawable.img16,
            R.drawable.img17,
            R.drawable.img18,
            R.drawable.img19,
            R.drawable.img20,
            R.drawable.img21,
            R.drawable.img22,
            R.drawable.img23,
            R.drawable.img24,
            R.drawable.img25,
            R.drawable.img26,
            R.drawable.img27,
            R.drawable.img28,
            R.drawable.img29,
    };//用数组储存要显示的图片
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Bitmap bm = BitmapFactory.decodeResource(this.getResources(),imgRes[count]);//加载的图片资源对象//加载的图片资源id
        //Log.d(msg,"加载图片资源");
        img = (ImageView)findViewById(R.id.imageView);
        img.setImageBitmap(bm);//设置ImageView控件显示的图片
        but1 = (Button) findViewById(R.id.button0);//用于“下一张”的监听器
        but1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count++;
                if (count == 6) {
                    count = 0;//重置，即播放到最后一个图片时，重置数组下标，使最后一个图片的下一个图片为第一个
                }
                BitmapFactory.Options opt = new BitmapFactory.Options();
                opt.inSampleSize = 2;                   /*2倍缩放图片*/
                opt.inDither = false;                     /*不进行图片抖动处理*/
                opt.inPreferredConfig = null;             /*设置让解码器以最佳方式解码*/
                mAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.new_anim);//加载xml格式的动画配件（alpha）
                img.startAnimation(mAnimation);//动画启动
                Bitmap bm2 = BitmapFactory.decodeResource(MainActivity.this.getResources(), imgRes[count], opt);
                img.setImageBitmap(bm2);//切换至下一个图片
            }
        });
        but2 = (Button)findViewById(R.id.star);//用于“播放”的监听器，打开音乐同时开启循环播放
        but2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Log.d(msg1,"线程启动");
                        mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.bgmusic);//MediaPlayer.create(Context context, int resid)
                        mediaPlayer.start();
                        while (true) {
                            Message msg =  new Message();
                            msg.arg1 = count;
                            Log.d(msg1,"数据发送");
                            handler.sendMessage(msg);
                            count ++;
                            Log.d(msg1,"返回");
                            if(count >= imgRes.length){
                                count = 0;
                            }
                            try {
                                Thread.sleep(2000);//具体应用时可以通过传参数的方法
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();
            }
            @SuppressLint("HandlerLeak")
            public final Handler handler = new Handler() {
                public void handleMessage(Message msg) {
                    //img.setBackgroundResource(imgRes[(Integer) msg.obj]);//将图片设置为View背景（会放大）
                    BitmapFactory.Options opt = new BitmapFactory.Options();
                    opt.inSampleSize = 2;                   /*2倍缩放图片*/
                    opt.inDither = false;                     /*不进行图片抖动处理*/
                    opt.inPreferredConfig = null;             /*设置让解码器以最佳方式解码*/
                    mAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.new_anim);//加载xml格式的动画配件（alpha）
                    img.startAnimation(mAnimation);//动画启动，图片渐变
                    Bitmap bm2 = BitmapFactory.decodeResource(MainActivity.this.getResources(), imgRes[msg.arg1], opt);
                    img.setImageBitmap(bm2);//切换至下一个图片
                }
            };

        });
        but4 = (Button)findViewById(R.id.button);//用于“上一张// ”的监听器
        but4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count--;
                if (count == -1) {
                    count = 6;//重置
                }
                BitmapFactory.Options opt = new BitmapFactory.Options();
                opt.inSampleSize = 2;                   /*2倍缩放图片*/
                opt.inDither = false;                     /*不进行图片抖动处理*/
                opt.inPreferredConfig = null;             /*设置让解码器以最佳方式解码*/
                mAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.new_anim);//加载xml格式的动画配件（alpha）
                img.startAnimation(mAnimation);//动画启动，图片渐变
                Bitmap bm2 = BitmapFactory.decodeResource(MainActivity.this.getResources(), imgRes[count], opt);
                img.setImageBitmap(bm2);//切换至上一个图片
            }
        });
    }
}